<template>
	<view class="ljapp">
		<view v-if="loading">
		</view>
		<view v-else>
			<view style="padding-bottom: 38rpx;">
				<lj-top-tip :type="status_txt" :text="reason" v-if="status_txt"></lj-top-tip>

				<view class="card">
					<view class="title">请如实填写机构真实信息</view>

					<u--form labelWidth="auto" :labelStyle="{fontSize:'28rpx',color:'#333'}">
						<u-form-item label="推荐码" borderBottom>
							<u--input
								v-model="obj.invite"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="请输入推荐码"
							></u--input>
						</u-form-item>

						<u-form-item label="机构名称" required borderBottom>
							<u--input
								v-model="obj.name"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="药房、医院、门诊名称"
							></u--input>
						</u-form-item>

						<u-form-item label="机构地址" required borderBottom>
							<view class="u-flex" style="width:100%" @click="map">
								<view class="u-flex-fill" style="margin-right: 10rpx;">
									<u--input
										v-model="obj.address"
										border="none"
										inputAlign="right"
										fontSize="24rpx"
										placeholderStyle="color: #c2c2c2"
										placeholder="选择您机构地址（定位）"
										disabled
										disabledColor="transparent"
									></u--input>
								</view>
								<u-icon name="map" color="#12b3f1"></u-icon>
							</view>
						</u-form-item>

						<u-form-item label="联系人" required borderBottom>
							<u--input
								v-model="obj.realname"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="请输入联系人"
							></u--input>
						</u-form-item>

						<u-form-item label="联系方式" required borderBottom>
							<u--input
								v-model="obj.telephone"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="联系电话"
							></u--input>
						</u-form-item>

						<u-form-item label="执照编码" required borderBottom>
							<u--input
								v-model="obj.licensecode"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="医疗机构许可证或营业执照编码"
							></u--input>
						</u-form-item>

						<view class="u-flex u-flex-between" style="margin-top: 22rpx;">
							<view>
								<u-form-item label="执照照片" required>
								</u-form-item>
								<u-upload
									:fileList="upload_licensepic"
									maxCount="1"
									uploadText="上传照片仅限1张"
									@afterRead="uploadLicensepicSuccess"
									@delete="upload_licensepic=[],obj.licensepic=''"
								></u-upload>
							</view>

							<view>
								<u-form-item label="机构门头照" required>
								</u-form-item>
								<u-upload
									:fileList="upload_avatar"
									maxCount="1"
									uploadText="上传照片仅限1张"
									@afterRead="uploadAvatarSuccess"
									@delete="upload_avatar=[],obj.avatar=''"
								></u-upload>
							</view>
						</view>

					</u--form>
				</view>

				<view class="card" style="padding-top: 34rpx;padding-bottom: 56rpx;">
					<view class="title">设置后台登录账号密码</view>
					<u--form labelWidth="auto" :labelStyle="{fontSize:'28rpx',color:'#333'}">
						<u-form-item label="账号" required borderBottom>
							<u--input
								v-model="obj.username"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="设置后台登录账号"
							></u--input>
						</u-form-item>

						<u-form-item label="密码" required borderBottom>
							<u--input
								v-model="obj.password"
								border="none"
								inputAlign="right"
								fontSize="24rpx"
								placeholderStyle="color: #c2c2c2"
								placeholder="设置后台登录密码至少六位数"
							></u--input>
						</u-form-item>

					</u--form>
				</view>

				<view class="btn-row" v-if="status!=1">
					<view class="btn" @click="submit">{{status==0||status==2?'修改提交':'提交申请'}}</view>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				status: -10, // -10未申请 0.待审核 1.审核通过
				status_txt: '',
				reason: '',
				time: '',

				//提交需求
				obj: {
					invite: '',
					name: '',
					address: '',
					realname: '',
					telephone: '',
					licensecode: '',
					licensepic: '',
					avatar: '',

					username: '',
					password: '',
				},
				upload_licensepic: [],
				upload_avatar: [],
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			async uploadLicensepicSuccess(event) {
				this.upload_licensepic = [{
					...(event.file),
					status: 'uploading',
					message: '上传中'
				}]

				const result = await this.uploadFilePromise(this.upload_licensepic[0].url)
				if(result.code == 1){
					this.upload_licensepic = [{
						status: 'success',
						message: '',
						url: result.data.fullurl
					}]
					this.obj.licensepic = result.data.url
				}else{
					this.upload_licensepic = [{
						status: 'failed',
						message: '上传失败'
					}]
				}
			},
			async uploadAvatarSuccess(event) {
				this.upload_avatar = [{
					...(event.file),
					status: 'uploading',
					message: '上传中'
				}]

				const result = await this.uploadFilePromise(this.upload_avatar[0].url)
				if(result.code == 1){
					this.upload_avatar = [{
						status: 'success',
						message: '',
						url: result.data.fullurl
					}]
					this.obj.avatar = result.data.url
				}else{
					this.upload_avatar = [{
						status: 'failed',
						message: '上传失败'
					}]
				}
			},
			uploadFilePromise(url, uploadurl) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.$Config.http_static+'api/common/uploader',
						filePath: url,
						name: 'file',
						formData: {},
						success: (uploadFileRes) => {
							var res = JSON.parse(uploadFileRes.data);
							console.log(res)
							resolve(res)
						},
						fail: e => {
							resolve({code:0})
						},
					});
				})
			},
			map() {
			    var that = this;

				// var res = {
				// 	address: "广东省深圳市龙华区龙华民治民乐新村",
				// 	errMsg: "chooseLocation:ok",
				// 	latitude: 22.593379,
				// 	longitude: 114.055481,
				// 	name: "丰泽湖山庄",
				// }
				// that.obj.address = res.address
				// that.obj.lat = res.latitude;
				// that.obj.lng = res.longitude;
				// return false

			    uni.chooseLocation({
			        geocode: true,
			        success: function(res) {
						if(!res.address) return
						console.log(res)
						that.obj.address = res.address
						that.obj.lat = res.latitude;
						that.obj.lng = res.longitude;
			        }
			    });
			},

			//详情
			init() {
				this.$Http({
					url: 'businessApplyDetail',
					load: false,
					data: {},
				}).then(res => {
					this.loading = false
					if (res.code == 1) {
						this.obj.invite = res.msg//res.data.invite
						if(res.data){
							this.status = res.data.auditstatus
							if(this.status==0){
								this.status_txt = 'warning'
								this.reason = '审核中'
								this.time = this.$u.timeFormat(res.data.updatetime)
							}else if(this.status==1){
								this.status_txt = 'success'
								this.reason = '已通过'
								this.time = this.$u.timeFormat(res.data.audittime)
							}else if(this.status==2){
								this.status_txt = 'danger'
								this.reason = '未通过，原因：'+res.data.reason
								this.time = this.$u.timeFormat(res.data.audittime)
							}

							// if(res.data.status==1){

							// }else{
								// this.obj.invite = res.data.invite
								this.obj.name = res.data.name
								this.obj.address = res.data.address
								this.obj.lat = res.data.lat
								this.obj.lng = res.data.lng
								this.obj.realname = res.data.realname
								this.obj.telephone = res.data.telephone
								this.obj.licensecode = res.data.licensecode
								this.obj.licensepic = res.data.licensepic
								this.obj.avatar = res.data.avatar
								this.obj.username = res.data.username
								this.obj.password = res.data.password

								if (this.obj.licensepic) this.upload_licensepic = [{
									status: 'success',
									message: '',
									url: this.$base.cdn(this.obj.licensepic),
								}]
								if (this.obj.avatar) this.upload_avatar = [{
									status: 'success',
									message: '',
									url: this.$base.cdn(this.obj.avatar),
								}]
							// }
						}
					}
				});
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100,
				});
			},
			//提交
			submit() {
				this.$Http({
					url: 'businessApply',
					data: this.obj,
					show: true,
					type: 'POST'
				}).then(res => {
					console.log('submit',res)
					if (res.code == 1) {
						this.$u.toast(res.msg)
						this.init()
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import '@/pagesB/style.less';

	.card{
		margin: 20rpx;
		padding-top: 26rpx;
		padding-left: 16rpx;
		padding-right: 15rpx;
		padding-bottom: 40rpx;

		.title{
			position: relative;
			padding-left: 34rpx;
			height: 42rpx;
			line-height: 42rpx;
			font-weight: 500;
			margin-left: 20rpx;
			margin-bottom: 10rpx;

			&:after{
				position: absolute;
				top: 0;
				left: 0;
				content: '';
				width: 6rpx;
				height: 42rpx;
				border-radius: 42rpx;
				background-color: #3dc787;
				overflow: hidden;
			}
		}

		/deep/.u-form-item__body{padding: 24rpx 0 22rpx;}
		/deep/.u-form-item__body__left{padding-left: 52rpx!important;}
		/deep/.u-line{
			border-color: #f5f5f5!important;
			border-width: 2px!important;
		}
		/deep/.u-form-item__body__right__content{padding-right: 54rpx;}
		/deep/.u-form-item__body__left__content__required{font-weight: 700;left:-24rpx;color:#ff0707;}

		/deep/.u-upload{padding-left: 28rpx;}
		/deep/.u-upload__wrap__preview__image{
			width: 174rpx!important;height: 158rpx!important;
		}
		/deep/.u-upload__button{margin: 0;border-radius: 8rpx;background-color: #f1f1f1;width: 174rpx!important;height: 158rpx!important;
			.u-icon__icon{
				font-size: 66rpx!important;
			}
		}
		/deep/.u-upload__button__text{font-size: 22rpx; color: #999;}
	}

	.btn-row{
		margin-top: 50rpx;
	}
</style>
